package com.lefu.es.system;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lefu.es.adapter.UserListViewAdapter;
import com.lefu.es.constant.AppData;
import com.lefu.es.constant.BluetoothTools;
import com.lefu.es.constant.UtilConstants;
import com.lefu.es.entity.UserModel;
import com.lefu.es.service.ExitApplication;
import com.lefu.es.service.UserService;
import com.lefu.es.util.LogUtil;
import com.wellness.scale.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * 用户列表界面
 *
 * @author Leon
 *         2015-11-19
 */
public class UserListActivity extends Activity {

    public List<UserModel> users = new ArrayList<>();
    private UserListViewAdapter userAdapter;
    private ListView birthListView;
    private ImageView add_photo_imageView;
    private boolean isEdit = false;
    OnClickListener imgOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.addphoto_imageView:
                    //toAddUser();
                    break;
                case R.id.useredit_textView:
                    if (!isEdit) {
                        isEdit = true;
                    } else {
                        isEdit = false;
                    }
                    userAdapter.setEdit(isEdit);
                    userAdapter.notifyDataSetChanged();
                    break;
                case R.id.back_tv:
                    checkUser();
                    break;
                default:
            }
        }
    };
    private TextView editText;
    private TextView backText;
    private UserService uservice;
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothTools.ACTION_NO_USER.equals(action)) {
                toAddUser();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_edit_list);
        ButterKnife.bind(this);
        viewInit();
        ExitApplication.getInstance().addActivity(this);
    }

    public void viewInit() {
        add_photo_imageView = (ImageView) findViewById(R.id.addphoto_imageView);
        birthListView = (ListView) this.findViewById(R.id.user_listview);
        birthListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UtilConstants.CURRENT_USER = userAdapter.users.get(position);
                UtilConstants.CURRENT_SCALE = UtilConstants.CURRENT_USER.getScaleType();
                UtilConstants.SELECT_USER = UtilConstants.CURRENT_USER.getId();
                System.out.println("当前用户称类型：" + UtilConstants.CURRENT_SCALE);
                if (null != UtilConstants.CURRENT_USER && null != UtilConstants.CURRENT_USER.getDanwei() && !"".equals(UtilConstants.CURRENT_USER.getDanwei())) {
                    if (null != UtilConstants.su) {
                        UtilConstants.su.editSharedPreferences("lefuconfig", "unit", UtilConstants.CURRENT_USER.getDanwei());
                        UtilConstants.su.editSharedPreferences("lefuconfig", "user", UtilConstants.CURRENT_USER.getId());
                    }
                }
                LogUtil.e("****UtilConstants.CURRENT_USER-->" + UtilConstants.CURRENT_USER.toString());
                UtilConstants.CHOICE_KG = (UtilConstants.CURRENT_SCALE == null ||
                        !"".equals(UtilConstants.CURRENT_USER.getScaleType())) ?
                        UtilConstants.UNIT_KG : UtilConstants.CURRENT_USER.getScaleType();
                Intent intent = new Intent(UserListActivity.this, LoadingActivity.class);
                UserListActivity.this.startActivity(intent);
                // finish();
            }
        });

        editText = (TextView) findViewById(R.id.useredit_textView);
        editText.setOnClickListener(imgOnClickListener);
        backText = (TextView) findViewById(R.id.back_tv);
        backText.setOnClickListener(imgOnClickListener);
    }

    @Override
    protected void onStart() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothTools.ACTION_NO_USER);
        registerReceiver(broadcastReceiver, intentFilter);
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        dataInit();
        userAdapter = new UserListViewAdapter(getApplicationContext(), R.layout.user_list_item, users);
        birthListView.setAdapter(userAdapter);
        //viewInit();
    }

    public void dataInit() {
        try {
            if (null == uservice) {
                uservice = new UserService(this);
            }
            users.clear();
            users = this.uservice.getAllUserByScaleType();
        } catch (Exception e) {
            users = new ArrayList<>();
            Log.e(TAG, "用户列表程序失败" + e.getMessage());
        }
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(broadcastReceiver);
        ButterKnife.unbind(this);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            checkUser();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_HOME) {
        }
        return super.onKeyDown(keyCode, event);
    }

    /*** 检查是否还存在用户 ***/
    private void checkUser() {
        if (null == userAdapter.getItem(0)) {
            toAddUser();
        } else {
            if (null == UtilConstants.CURRENT_USER || AppData.isCheckScale) {
                Toast.makeText(UserListActivity.this, getString(R.string.user_select_need), Toast.LENGTH_SHORT).show();
            } else {
                UserListActivity.this.finish();
            }
        }
    }

    /*** 跳转到添加用户界面 ***/
    private void toAddUser() {
        Intent intent = new Intent(UserListActivity.this, UserAddActivity.class);
        intent.putExtra("flag_activity", 1000);
        startActivity(intent);
    }

    @OnClick(R.id.addphoto_imageView)
    public void saveClick() {
        toAddUser();
    }

}
